package qmm.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import dao.CartDao;
import qmm.dao.AddressDao;
import qmm.dao.impl.AddressDaoImpl;
import qmm.dao.impl.CartDaoImpl;
import qmm.entity.Address;
import qmm.entity.user;
import qmm.entity.vo.CartVo;

@WebServlet("/CartServlet")
public class CartServlet extends BaseServlet{
	CartDao cartDao = new CartDaoImpl();
	
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		user user = (user) session.getAttribute("user");
		Map<String,Object> map = new HashMap<String,Object>();
		if(user==null){
			map.put("msg", "nologin");
			String json = JSON.toJSONString(map);
			response.getWriter().write(json);
			return;
		}
		int goodsId = Integer.parseInt(request.getParameter("goodsId"));
		int num = Integer.parseInt(request.getParameter("num"));
		int n2 = cartDao.getGoodsId(goodsId, user.getId());
		int n = 0;
		if(n2>0){
			n = cartDao.updateNum(goodsId, num, user.getId());
		}else{
			n = cartDao.add(goodsId, num, user.getId());
		}
		if(n>0){
			int category = cartDao.getCount(user.getId());//获得购物车中商品种类数量
			List<CartVo> cartVoList = cartDao.findCartVo(user.getId());//获得用户购物商品信息
			double totalMoney = 0;
			for(CartVo vo:cartVoList){
				totalMoney+=vo.getPrice()*vo.getNum();
			}
			int totalNum = cartDao.getNum(user.getId());//总件数
			session.setAttribute("cartVoList", cartVoList);//购物车列表
			session.setAttribute("category", category);//种类数量
			session.setAttribute("totalMoney", totalMoney);//总价
			map.put("msg", "success");
			map.put("totalNum", totalNum);
			map.put("category", category);
			map.put("totalMoney", totalMoney);
			String json = JSON.toJSONString(map);
			response.getWriter().write(json);
		}else{
			map.put("msg", "fail");
			String json = JSON.toJSONString(map);
			response.getWriter().write(json);
		}
	}
	public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		user user = (user) session.getAttribute("user");
		
		String path = request.getParameter("path");
		
		int category = cartDao.getCount(user.getId());//获得购物车中商品种类数量
		List<CartVo> cartVoList = cartDao.findCartVo(user.getId());//获得用户购物商品信息
		double totalMoney = 0;
		for(CartVo vo:cartVoList){
			totalMoney+=vo.getPrice()*vo.getNum();
		}
		session.setAttribute("cartVoList", cartVoList);//购物车列表
		session.setAttribute("category", category);//种类数量
		session.setAttribute("totalMoney", totalMoney);//总价
		response.sendRedirect(path+".jsp");
	}
	
	public void searchSelect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		user user = (user) session.getAttribute("user");
		
		String ids = request.getParameter("ids");
		
		List<CartVo> cartVoList = cartDao.findSelect(ids);
		double totalMoney = 0;
		for(CartVo vo:cartVoList){
			totalMoney+=vo.getPrice()*vo.getNum();
		}
		session.setAttribute("cartList", cartVoList);//ﳵб
		session.setAttribute("totalMoney", totalMoney);//ܼ
		
		//查询用户默认收货地址
				AddressDao dao = new AddressDaoImpl();
				Address address = dao.getDefaultAddress(user.getId());
				session.setAttribute("defaultAddress", address);
				
		response.getWriter().print("success");
	}
	
	public void updateNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		user user = (user) session.getAttribute("user");
		
		int id = Integer.parseInt(request.getParameter("id"));
		int num = Integer.parseInt(request.getParameter("num"));
		cartDao.updateNum(id, num);
		
		List<CartVo> cartVoList = cartDao.findCartVo(user.getId());//获得用户购物商品信息
		double totalMoney = 0;
		for(CartVo vo:cartVoList){
			totalMoney+=vo.getPrice()*vo.getNum();
		}
		session.setAttribute("cartVoList", cartVoList);//购物车列表
		session.setAttribute("totalMoney", totalMoney);//总价
		response.getWriter().print("success");
	}
	
	public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		user user = (user) session.getAttribute("user");
		
		String idStr = request.getParameter("id");
		String idsStr = request.getParameter("ids");
		int n = 0;
		if(idStr==null||idStr==""){//删除选中商品
			String[] ids = idsStr.split(",");
			for(String id:ids){
				n += cartDao.del(Integer.parseInt(id));
			}
		}else{//单个删除
			n = cartDao.del(Integer.parseInt(idStr));
		}
		if(n>0){
			List<CartVo> cartVoList = cartDao.findCartVo(user.getId());//获得用户购物商品信息
			double totalMoney = 0;
			for(CartVo vo:cartVoList){
				totalMoney+=vo.getPrice()*vo.getNum();
			}
			session.setAttribute("cartVoList", cartVoList);//购物车列表
			session.setAttribute("totalMoney", totalMoney);//总价
		}
		response.sendRedirect("cart.jsp");
	}
}